package models

import (
	"strconv"
	"strings"

	"github.com/astaxie/beego"
)

type Xxp_shop struct {
	Id          int64
	Kol_id      int64  //店铺用户id
	Name        string //店铺名称
	Shop_str    string //店铺分享id
	Description string //店铺简介
	Head_img    string //头像
	Recommend   string //推荐商品id
	Status      int    //状态
	Created     int64
	Modified    int64
}

func ShopDetail(uid int) Response {
	var ret Response
	var shop Xxp_shop
	shop.Kol_id = int64(uid)
	ormer.Raw("SELECT * FROM `xxp_shop` WHERE kol_id = ? LIMIT 1 ", uid).QueryRow(&shop)

	if shop.Id <= 0 {
		ret.Code = 100200
		ret.Msg = "店铺不存在"
		return ret
	}
	ret.Data = shop
	return ret
}

func ShopGetDetail(uid int) Xxp_shop {
	var shop Xxp_shop
	shop.Kol_id = int64(uid)
	ormer.Raw("SELECT * FROM `xxp_shop` WHERE kol_id = ? LIMIT 1 ", uid).QueryRow(&shop)
	return shop
}

func ShopUpdate(uid int, name, description, head_img string) Response {
	var ret Response
	var shop Xxp_shop
	var id int64
	ormer.Raw("select id from xxp_shop where kol_id = ?", uid).QueryRow(&id)
	shop.Id = id
	ormer.Read(&shop)

	if name != "" {
		shop.Name = name
	} else if description != "" {
		shop.Description = description
	} else if head_img != "" {
		shop.Head_img = head_img
	} else {
		return ret
	}
	_, err := ormer.Update(&shop)
	if err != nil {
		beego.Debug("数据更新错误", err.Error())
		ret.Code = ERRCODE_DATABASE_ERROR
		ret.Msg = "数据更新错误"
		return ret
	}
	ret.Msg = "修改成功"
	return ret
}

type shopGood struct {
	Xxp_goods
	Istop int
}

func ChangeStringToNum(str string) []int {
	aaa := strings.Split(str, ",")
	var num []int
	for _, v := range aaa {
		a, _ := strconv.ParseInt(v, 10, 64)
		num = append(num, int(a))
	}
	return num
}

func ChangeToNumString(num []int) string {
	var str string
	for k, v := range num {
		str += strconv.Itoa(v)
		if k+1 < len(num) {
			str += ","
		}
	}
	return str

}

func ShopGoods(uid, types, keystart int) (res Response) {
	share_url := beego.AppConfig.String("h5_share")
	var lists List
	var goods []Xxp_goods
	ormer.Raw("SELECT * FROM `xxp_goods` WHERE id IN(SELECT gid FROM `xxp_goods_kol` WHERE uid=?) AND types = ? LIMIT ?,10000", uid, types, 10000*keystart).QueryRows(&goods)

	var list []shopGood
	var shop Xxp_shop
	ormer.Raw("select * from xxp_shop where kol_id=?", uid).QueryRow(&shop)
	top := ChangeStringToNum(shop.Recommend)
	for _, v := range goods {
		var a1 shopGood
		a1.Xxp_goods = v
		for _, v1 := range top {
			if v1 == v.Id {
				a1.Istop = 1
			}
			a1.Base_price = 0
		}
		a1.Share = share_url + "/detail?id=" + strconv.Itoa(int(v.Id)) + "&sid=" + strconv.Itoa(int(shop.Id))
		list = append(list, a1)
	}
	keystart = keystart + 1
	if len(list) <= 0 {
		lists.List = []string{}
	} else {
		lists.List = list
	}
	//判断是否有下一页
	var a []Xxp_goods
	n, _ := ormer.Raw("SELECT * FROM `xxp_goods` WHERE id IN(SELECT gid FROM `xxp_goods_kol` WHERE uid=?) AND types = ? LIMIT ?,10000", uid, types, 10000*keystart).QueryRows(&a)
	if n > 0 {
		lists.Hasnext = 1
	}
	lists.Keystart = keystart
	res.Data = lists
	return
}

func ShopGoodsAddTop(uid, gid int) (res Response) {
	var topstr string
	var topid []int
	err := ormer.Raw("SELECT recommend FROM `xxp_shop` WHERE kol_id = ?", uid).QueryRow(&topstr)
	if err != nil {
		res.Code = ERRCODE_DATABASE_ERROR
		res.Msg = "数据出错"
		return
	}
	if topstr != "" {
		topid = ChangeStringToNum(topstr)

	}
	if len(topid) >= 3 {
		res.Code = 100500
		res.Msg = "做多添加3条置顶商品"
		return
	}
	topid = append(topid, gid)
	topstr = ChangeToNumString(topid)
	_, err = ormer.Raw("UPDATE `xxp_shop` SET `recommend`=? WHERE kol_id = ?", topstr, uid).Exec()
	if err != nil {
		res.Code = ERRCODE_DATABASE_ERROR
		res.Msg = "数据出错"
		return
	}
	res.Msg = "置顶成功"
	return
}

func ShopGoodsDelTop(uid, gid int) (res Response) {
	var topstr string
	var topid []int
	err := ormer.Raw("SELECT recommend FROM `xxp_shop` WHERE kol_id = ?", uid).QueryRow(&topstr)
	if err != nil {
		res.Code = ERRCODE_DATABASE_ERROR
		res.Msg = "数据出错"
		return
	}
	if topstr != "" {
		topid = ChangeStringToNum(topstr)

	} else {
		res.Code = 100600
		res.Msg = "取消失败，置顶不存在"
		return
	}
	for k, v := range topid {
		if v == gid {
			topid = append(topid[:k], topid[k+1:]...)
			break
		}
	}

	topstr = ChangeToNumString(topid)
	_, err = ormer.Raw("UPDATE `xxp_shop` SET `recommend`=? WHERE kol_id = ?", topstr, uid).Exec()
	if err != nil {
		res.Code = ERRCODE_DATABASE_ERROR
		res.Msg = "数据出错"
		return
	}
	res.Msg = "取消置顶成功"
	return
}
